﻿--[[
   @brief    
   @author   rickonzhang
   @version  1.0
   @date     2015-04-01
--]]

game_menue_control =
{
    init = function ()
        ierd_tgp.tpf_ui.Require("game_menue\\view.lua");
        ierd_tgp.tpf_ui.Require("game_menue\\model.lua");
    end;

    open_game_menue = function(event_id, json_str)
        local param_table = base_utility.json_to_table(json_str);
        game_menue_view.show(param_table['xoffset'], param_table['yoffset'], param_table['width'], param_table['height'], param_table['id']);
    end;

    hide_game_menue = function(event_id, json_str)
        game_menue_view.hide();
    end;

    get_sub_account_list = function()
        local loginlib =
            base_utility.get_component_adapter("ILogin_mgr",
                "ierd_tgp.login_mgr.GetLoginMgr();");
        subacclist = ierd_tgp.login_mgr.SubAccountListPod:new_local();
        loginlib:GetSubAccountList(subacclist);
        ierd_tgp.tpf_ui.Log("[game_menue_control]subacclist "..subacclist.count);
        for i = 1, subacclist.count do 
            ierd_tgp.tpf_ui.Log("[game_menue_control]subacclist uin:"..subacclist.uins[i-1]);
        end;
        return subacclist;
    end;

    on_sub_account_login_success = function(event_id, json_str)
        --ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        if sub_account_view ~= nil then
            sub_account_view.on_sub_account_login_success(json_str);
        end;
    end;

    on_sub_account_del_ret = function(event_id, json_str)
        --ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        if sub_account_view ~= nil then
            sub_account_view.on_sub_account_del_ret(json_str);
        end;
    end;

    on_main_frame_pos_change = function(event_id, json_str)
        --ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        if sub_account_view ~= nil then
            sub_account_view.on_main_frame_pos_change();
        end;
    end;

    on_switch_account_list_dlg = function(event_id, json_str)
        ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        if sub_account_view ~= nil then
            sub_account_view.on_switch_account_list_dlg(json_str);
        end;
    end;

    on_banner_focus_change = function(event_id, json_str)
        ierd_tgp.tpf_ui.Require("game_menue\\sub_account_view.lua");
        if sub_account_view ~= nil then
            sub_account_view.on_banner_focus_change(json_str);
        end;
    end;

    on_mainframe_mode_change = function(event_id, json_str)
        if sub_account_view ~= nil then
            sub_account_view.on_mainframe_mode_change(json_str);
        end;

        if json_str ~= nil then
            local params = base_utility.json_to_table(json_str);
            if nil ~= params and nil ~= params.mainframe_mode then
                game_menue_model.cur_main_mode = params.mainframe_mode;
            end
        end
    end;

    register_main_frame_pos_change_evt = function()
        event_center.add_listener(mainframe_event.WINDOW_POSITION_CHANGE, 
            "game_menue_control.on_main_frame_pos_change");
    end;

    open_game_community = function(event_id, json)
        if nil == json then
            return;
        end
        base_utility.log(string.format("[open_game_update_detail]json:%s", json));
        local param = base_utility.json_to_table(json);
        if nil ~= param and nil ~= param.data and nil ~= param.data.game_id then
            local game_id = tonumber(param.data.game_id);

            if game_id == GAME_ID.GAME_ID_LOL then
                local not_exist_param = {};
                not_exist_param.module_id = 55555;
                not_exist_param.module_url = 
                    LoadStr("OPEN_GAMECOMUTIY_URL")..
                    "game_id="..tostring(game_id).."&ADTAG=FORUM.BACKHOME";

                local exist_param = {};
                exist_param.module_id = GAME_ID.GAME_ID_LOL
                exist_param.module_url = LoadStr("OPEN_GAMECOMUTIY_LOL_URL");
                game_menue_control.jump_to_game_community(exist_param, not_exist_param);
            end

        end
    end;

    -- 跳转到游戏社区
    jump_to_game_community = function(exist_param, not_exist_param)
        local jump_param = {};
        jump_param = exist_param;
        if nil == exist_param or nil == exist_param.module_id or nil == exist_param.module_url then
            return;
        end
        
        local game_exist = banner_control.get_game_visible_by_gameid(jump_param.module_id);
        if game_exist == false then
            if nil == not_exist_param or 
               nil == not_exist_param.module_id or 
               nil == not_exist_param.module_url then
                return;
            end
            jump_param = not_exist_param;
        end

        if game_menue_model.cur_main_mode == 1 or 
           (nil ~= wegame_mainframe_model and wegame_mainframe_model.is_need_init_frame == true) then
            wegame_mainframe_control.set_jump_module_web_json(base_utility.table_to_json(jump_param));
        end
        
        event_center.send_event(mainframe_event.SHOW_MAIN_WND, "");

        event_center.send_event(banner_event.JUMP_MODULE_WEB, base_utility.table_to_json(jump_param));
        game_menue_view.hide();
    end;

    -- 执行游戏操作
    excute_game_act = function(game_action, game_id, server_id)
        base_utility.log("[game_menue_control]excute_game_act, action:%d, game_id, %d", game_action, game_id);
        local zone_id = server_id;
        if zone_id == nil then
            zone_id = 0;
        end;
        if uin == nil then
            uin = 0;
        end;
        local action_info = {
                  game_id_ = game_id,
                  zone_id_ = zone_id,
                  version_ = 0,
                  uid_ = login_model.main_uin,
                  launch_op_from = ierd_tgp.game_library.LAUNCH_OP_FROM_LANDING_PAGE
              };
        WGGameAction.excute_action(game_action, action_info);
    end; 

};

game_menue_control.init();
